<template>
  <div>
    <div class="strategyFatherBox">
      <el-button type="success" size='small' @click="dialogVisible()" class="addClick">{{$t('添加')}}</el-button>
      <el-button type="danger" size='small' @click="deleteData" class="delClick">{{$t('删除')}}</el-button>
    </div>
    <el-table size='mini' ref="multipleTable" :data="serverLogList" tooltip-effect="dark" style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column type="selection" align='center' min-width="55"></el-table-column>
      <el-table-column prop="name" align='center' :label="$t('名称')" min-width="160"></el-table-column>
      <el-table-column prop="status" align='center' :formatter="statusFormat" :label="$t('是否启用远程日志存储')" min-width="160"></el-table-column>
      <el-table-column prop="type" align='center' :label="$t('外发格式')" min-width="160">
        <template slot-scope="scope">
          {{ $t(typeList[scope.row.type]) || '' }}
        </template>
      </el-table-column>
      <el-table-column prop="lv" align='center' :formatter="levelFormat" :label="$t('日志级别')" min-width="160"></el-table-column>
      <el-table-column prop="addr" align='center' :label="$t('地址')" min-width="160"></el-table-column>
      <el-table-column prop="port" align='center' :label="$t('端口')" min-width="160"></el-table-column>
      <el-table-column align='center' width='160' fixed="right" :label="$t('操作')">
        <template slot-scope="scope">
          <el-button type="success" plain size="mini" @click="dialogVisible(scope.row, 'edit')" :class="'edit' + (scope.$index + 1)">{{$t('编辑')}}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <RemoteSyslogConfigDialog v-if="remoteSyslogConfigDialogVisible" ref="refRemoteSyslogConfigDialogVisible"></RemoteSyslogConfigDialog>
  </div>
</template>
<script>

import { getServiceLogArray, delServiceLog } from '@/api/system/log'
import RemoteSyslogConfigDialog from './components/RemoteSyslogConfigDialog'
export default {
  name: 'ConfigManager',
  components: {
    RemoteSyslogConfigDialog
  },
  data () {
    return {
      remoteSyslogConfigDialogVisible: false,
      serverLogList: [],
      nameStr: '',
      statusList: { 'on': '启用', 'off': '禁用' },
      levelList: {
        1: 'ERROR',
        2: 'ALERT',
        3: 'INFO',
        4: 'DEBUG'
      },
      typeList: {
        'normal': '默认',
        'power': '电力格式',
        'json': 'json'
      }
    }
  },
  created () {
    this.listData()
  },
  methods: {
    statusFormat (data) {
      return this.$t(this.statusList[data.status])
    },
    levelFormat (data) {
      return this.levelList[data.lv]
    },
    // 多选按钮
    handleSelectionChange (data) {
      var nameStr = ''
      for (var item of data) {
        if (nameStr === '') {
          nameStr = item.name
        } else {
          nameStr = nameStr + '@#@' + item.name
        }
      }
      this.nameStr = nameStr
    },
    // 列表
    listData () {
      getServiceLogArray().then(res => {
        if (res.status === 0) {
          this.serverLogList = res.info
        }
      })
    },
    // 添加按钮
    dialogVisible (data, type) {
      this.remoteSyslogConfigDialogVisible = true
      this.$nextTick(() => {
        this.$refs.refRemoteSyslogConfigDialogVisible.init(data, type)
      })
    },
    deleteData () {
      if (this.nameStr) {
        this.$confirm(this.$t('您是否要删除所有选中的数据？'), this.$t('提示'), {
          closeOnClickModal: false,
          confirmButtonText: this.$t('确定'),
          cancelButtonText: this.$t('取消'),
          confirmButtonClass: 'confirmButton',
          cancelButtonClass: 'confirmCancelButton',
          type: 'warning'
        }).then(() => {
          delServiceLog({ name: this.nameStr }).then(res => {
            if (res.status === 0) {
              this.$message({
                message: this.$t('删除成功'),
                type: 'success'
              })
              this.nameStr = ''
              this.listData()
            }
          })
        })
      } else {
        this.$messages.error(this.$t('请至少选择一条数据进行删除'))
      }
    }
  }
}
</script>
